package logic

import (
	"context"
	"fmt"
	"nove/models/mysql"
	"strings"

	"nove/noverpc/internal/svc"
	"nove/noverpc/noverpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *noverpc.CommentAddRequest) (*noverpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	if strings.Contains(in.Content, "抄袭") || strings.Contains(in.Content, "涉政") {
		return nil, fmt.Errorf("评论有敏感词，已拦截")
	}

	comment := mysql.Comment{
		Content: in.Content,
		UserId:  int(in.UserId),
		BookId:  int(in.BookId),
	}
	mysql.DB.Debug().Create(&comment)
	return &noverpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
